<template>
  <div class="type-nav">
    <div class="container">
      <div @mouseenter="mouseIsInNav=true" @mouseleave="mouseIsInNav=false">
        <h2 class="all">全部商品分类</h2>
        <div class="sort" v-show="categoryIsShow">
          <div
            class="all-sort-list2"
            @mouseenter="mouseIsInCategory = true"
            @mouseleave="mouseIsInCategory = false"
            @click="toSearch"
          >
            <div
              class="item"
              v-for="(Category1, index) in Category1List"
              :key="Category1.id"
              :class="{ active: mouseEnterIndex === index }"
              @mouseenter="category1MouseEnterThrottle(index, Category1)"
              @mouseleave="mouseEnterIndex = -1"
            >
              <h3>
                <a
                  :data-categoryname="Category1.name"
                  :data-category1id="Category1.id"
                  >{{ Category1.name }}</a
                >
              </h3>
              <div class="item-list clearfix">
                <div class="subitem">
                  <dl
                    class="fore"
                    v-for="Category2 in Category1.children"
                    :key="Category2.id"
                  >
                    <dt>
                      <a
                        :data-categoryname="Category2.name"
                        :data-category2id="Category2.id"
                        >{{ Category2.name }}</a
                      >
                    </dt>
                    <dd>
                      <em
                        v-for="Category3 in Category2.children"
                        :key="Category3.id"
                      >
                        <a
                          :data-categoryname="Category3.name"
                          :data-category3id="Category3.id"
                          >{{ Category3.name }}</a
                        >
                      </em>
                    </dd>
                  </dl>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
       <nav class="nav">
          <a href="###">服装城</a>
          <a href="###">美妆馆</a>
          <a href="###">尚品汇超市</a>
          <a href="###">全球购</a>
          <a href="###">闪购</a>
          <a href="###">团购</a>
          <a href="###">有趣</a>
          <a href="###">秒杀</a>
        </nav>
    </div>
  </div>
</template>

<script>
import {
  reqCategory1List,
  reqCategory2List,
  reqCategory3List,
} from "@/api/home.js";
import { throttle } from "lodash";
export default {
  name: "Nav",
  data() {
    return {
      //1.分类列表
      Category1List: [],
      //2.保存当前鼠标移入一级列表的下标
      mouseEnterIndex: -1,
      //3.保存当前鼠标是否在分类区域
      mouseIsInCategory: false,
      //4.是否显示分类列表
      mouseIsInNav:false
    };
  },
  mounted() {
    //初始化1:发送一级列表请求
    this.getCategory1List();
    //初始化2:节流方法2
    this.category1MouseEnterThrottle = throttle(this.category1MouseEnter, 500, {
      //leading:让事件函数在节流开始前执行
      leading: true,
      //trailing配置项让函数在节流结束后执行最后一次
      trailing: true,
    });
  },
  methods: {
    //方法1: 请求一级分类列表
    async getCategory1List() {
      const result = await reqCategory1List();
      this.Category1List = result;
    },
    //方法2: 鼠标移入一级列表事件(请求二级列表)
    async category1MouseEnter(index, Category1) {
      //看门狗:判断当前鼠标若不在分类区域 则退出
      if (!this.mouseIsInCategory) return;
      //保存移入的一级列表的下标
      this.mouseEnterIndex = index;
      //判断:如果children属性存在,则不再发送对应的二级请求
      if (Category1.children) return;
      //发送二级列表请求
      const result = await reqCategory2List(Category1.id);
      //[插播]请求每个二级列表的三级列表
      result.forEach(async (item) => {
        const result = await reqCategory3List(item.id);
        this.$set(item, "children", result);
      });
      //给每个一级列表添加children属性,存放对应的二级列表
      this.$set(this.Category1List[index], "children", result);
    },
    //方法3: 是方法2的占位节流函数
    category1MouseEnterThrottle() {},
    //方法4: 分类列表的点击事件跳转
    toSearch(e) {
      console.dir(e.target.dataset, "点击元素");
      //先判断当前点击的target元素上有没有 data-categoryname属性,如果没有说明点击的位置不需要跳转 直接return
      if (!e.target.dataset.categoryname) return;
      //解构当前点击的元素的自定义属性信息
      const { category1id, category2id, category3id, categoryname } =
        e.target.dataset;
      this.$router.push({
        name: "Search",
        query: {
          category1id: category1id,
          category2id: category2id,
          category3id: category3id,
          categoryname: categoryname,
        },
      });
      //如果点击跳转到了搜索页面,则把分类列表隐藏
      this.mouseIsInNav = false
    },
  },

  computed:{
    //设置三级分类列表区域是否展示
    categoryIsShow() {
      //首先判断当前是否在首页,如果在首页则永久展示
      if (this.$route.name === "Home") return true;

      //判断鼠标在不在三级分类区域,如果在则返回true,否则返回false
      return this.mouseIsInNav;
    },
  }
};
</script>

<style scoped lang="less">
.type-nav {
  border-bottom: 2px solid #e1251b;
  .container {
    width: 1200px;
    margin: 0 auto;
    display: flex;
    position: relative;

    .all {
      width: 210px;
      height: 45px;
      background-color: #e1251b;
      line-height: 45px;
      text-align: center;
      color: #fff;
      font-size: 14px;
      font-weight: bold;
    }

    .nav {
      a {
        height: 45px;
        margin: 0 22px;
        line-height: 45px;
        font-size: 16px;
        color: #333;
      }
    }

    .sort {
      position: absolute;
      left: 0;
      top: 45px;
      width: 210px;
      height: 461px;
      position: absolute;
      background: #fafafa;
      z-index: 999;

      .all-sort-list2 {
        .item {
          &.active {
            h3 {
              background: #ccc;
            }
          }
          h3 {
            line-height: 30px;
            font-size: 14px;
            font-weight: 400;
            overflow: hidden;
            padding: 0 20px;
            margin: 0;

            a {
              color: #333;
            }
          }

          .item-list {
            display: none;
            position: absolute;
            width: 734px;
            min-height: 460px;
            background: #f7f7f7;
            left: 210px;
            border: 1px solid #ddd;
            top: 0;
            z-index: 9999 !important;

            .subitem {
              float: left;
              width: 650px;
              padding: 0 4px 0 8px;

              dl {
                border-top: 1px solid #eee;
                padding: 6px 0;
                overflow: hidden;
                zoom: 1;

                &.fore {
                  border-top: 0;
                }

                dt {
                  float: left;
                  width: 54px;
                  line-height: 22px;
                  text-align: right;
                  padding: 3px 6px 0 0;
                  font-weight: 700;
                }

                dd {
                  float: left;
                  width: 415px;
                  padding: 3px 0 0;
                  overflow: hidden;

                  em {
                    float: left;
                    height: 14px;
                    line-height: 14px;
                    padding: 0 8px;
                    margin-top: 5px;
                    border-left: 1px solid #ccc;
                  }
                }
              }
            }
          }

          &:hover {
            .item-list {
              display: block;
            }
          }
        }
      }
    }
  }
}
</style>
